﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Resources;

public partial class giao_dien_Login : System.Web.UI.Page
{
    //static ResourceManager resManager;
    //static CultureInfo cul;
    static HttpCookie cookie = new HttpCookie("Language");
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            try
            {
                //SetCurrentPage();
                if (cookie.Value == null)
                {
                    Response.Cookies.Add(cookie);
                }


                //check remember me cookie
                if (Request.Cookies["RememberMe"] != null)
                {
                    HttpCookie cookieRememberMe = Request.Cookies.Get("RememberMe");
                    txtName.Text = cookieRememberMe.Values["UserName"].ToString();
                    txtPass.Attributes.Add("Value", cookieRememberMe.Values["Password"].ToString());

                    if (cookieRememberMe.Values["CheckBox"].ToString() == "True")
                    {
                        cbRemember.Checked = true;
                    }
                    else
                    {
                        cbRemember.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                           
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        {
            string username = txtName.Text;
            string password = txtPass.Text;

            CustomerBus customerBus = new CustomerBus();

            if (customerBus.IsValidLogin(username, password))
            {
                lblLoginFail.Visible = false;

                //khai bao bien cookie
                HttpCookie cookieRememberMe = new HttpCookie("RememberMe");

                Session["username"] = username;

                //neu chon check box remember
                if (cbRemember.Checked == true)
                {
                    //xoa cookie cu
                    if (Request.Cookies["RememberMe"] != null)
                    {
                        cookieRememberMe = new HttpCookie("RememberMe");
                        cookieRememberMe.Values.Add("UserName", "");
                        cookieRememberMe.Values.Add("Password", "");
                        cookieRememberMe.Values.Add("CheckBox", "");
                        //gan thoi gian song cua cookie la 30 ngay 
                        cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                        //them cookie
                        Response.Cookies.Add(cookieRememberMe);
                    }

                    //luu cookie
                    cookieRememberMe = new HttpCookie("RememberMe");
                    cookieRememberMe.Values.Add("username", txtName.Text.Trim());
                    cookieRememberMe.Value.Equals(username);
                    cookieRememberMe.Values.Add("password", txtPass.Text.Trim());
                    cookieRememberMe.Values.Add("CheckBox", "True");
                    Response.Write(txtName.Text);
                    //gan thoi gian song cua cookie la 30 ngay 
                    cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                    //them cookie
                    Response.Cookies.Add(cookieRememberMe);
                }
                else
                {
                    //delete cookie
                    if (Request.Cookies["RememberMe"] != null)
                    {
                        cookieRememberMe = new HttpCookie("RememberMe");
                        cookieRememberMe.Values.Add("UserName", txtName.Text.Trim());
                        cookieRememberMe.Values.Add("Password", "");
                        cookieRememberMe.Values.Add("CheckBox", "");
                        //gan thoi gian song cua cookie la 30 ngay 
                        cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                        //them cookie
                        Response.Cookies.Add(cookieRememberMe);
                    }
                }
                if (username == "adminadmin")
                {
                    Response.Redirect("../admin/login.aspx");
                }
                else
                {
                    Response.Redirect("../giao dien/Default.aspx");
                }

            }
            else
            {
                lblLoginFail.Text = "Invalid username or password !";
                lblLoginFail.Visible = true;
            }
        }
    }
}